import { Component, OnInit, ViewChild } from '@angular/core';
import { DatePipe } from '@angular/common';
import { environment } from '../../../../environments/environment';
import { HttpClient } from '@angular/common/http';
import { ExportFileService } from '../../../core/exportFile';
import { TableCheckPageComponent } from '../../../layout/components/tableCheckPage.component';

@Component({
    selector: 'app-technicalsupport',
    templateUrl: './technicalSupport.component.html',
    styleUrls: ['./technicalSupport.component.less']
})
export class TechnicalSupportComponent implements OnInit {
    @ViewChild('tcp', { static: true }) tcp: TableCheckPageComponent;
    searchParam: any = {};
    headData = [];
    plainData = [];
    operaList: any = [
        { addModal: false },
        { editModal: false },
        { delModal: false }
    ];
    tempData = [];
    orgNodes = [];
    treeNodeStyle = { 'max-width': '300px', 'max-height': '400px', 'overflow-y': 'auto', 'overflow-x': 'hidden' };
    apiUrl: any = {};
    exportUrl = '';

    constructor(
        private http: HttpClient,
        private exportFile: ExportFileService,
        private datePipe: DatePipe
    ) { }

    ngOnInit() {
        this.headData = [
            { "name": "专家库人员名称", "width": "10%" },
            { "name": "专家库人员技术领域", "width": "15%" },
            { "name": "专家库人员部门", "width": "15%" },
            { "name": "专家库人员职务", "width": "10%" },
            { "name": "专家库人员电话", "width": "10%" },
            { "name": "专家库人员手机", "width": "10%" },
            { "name": "专家库人员备注", "width": "" }
        ];
        this.plainData = ["person", "area", "orgName", "position", "phone", "cellphone", "remark"];
        this.apiUrl.add = environment.project + '/zjkSupport/add';
        this.apiUrl.edit = environment.project + '/zjkSupport/update';
        this.apiUrl.del = environment.project + '/zjkSupport/delete';
        this.loadList();
        this.loadOrg();
    }
    loadList() {
        this.tcp.loadList(environment.project + '/zjkSupport/query', '');
    }
    loadOrg() {
        this.http.get(environment.apiBase + '/csmis/getKmnOrgTree.jwt').subscribe((res: any) => {
            this.orgNodes = res;
        });
    }
    search() {
        let params = '';
        for (const item of Object.keys(this.searchParam)) {
            const data = this.searchParam[item];
            if (data || data === 0) {
                params += '&' + item + '=';
                if (data instanceof Date) {
                    params += this.datePipe.transform(data, 'yyyy-MM-dd');
                } else {
                    params += data;
                }
            }
        }
        this.exportUrl = params;
        this.tcp.loadList(environment.project + '/zjkSupport/query', params);
    }
    export() {
        this.exportFile.getDown(environment.project + '/zjkSupport/export?page=1&limit=20' + this.exportUrl, '专家库人员.xls');
    }
    modalShow(type) {
        this.tempData = this.tcp.getAllData();
        this.operaList[type] = true;
    }
    modalHide(type) {
        this.operaList[type] = false;
    }
}
